﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SpeedTest
{
	public partial class PanelSearch : UserControl
	{
		Data data = null;

        public event SearchEvent Search;

		public PanelSearch(Data data)
		{
			InitializeComponent();
			this.data = data;
		}

		//Search specified databases
		//Create a SearchParameters class
		//Pass it back to the main form to navigate to and edit

		private void chkSearchAll_CheckedChanged(object sender, EventArgs e)
		{
			if (chkSearchAll.Checked) 
			{
				for (int i = 0; i < lstSearchFields.Items.Count; i++)
				{
					lstSearchFields.SetItemChecked(i, true);
				}

				lstSearchFields.Enabled = false; 
			}
			else { lstSearchFields.Enabled = true; }
		}

		private void SearchAll()
		{

			if (String.IsNullOrEmpty(txtSearchString.Text) || lstSearchFields.CheckedItems.Count < 1)
			{
				MessageBox.Show("The specified search is invalid. Please specify a the Search Text and Search Location");
				return;
			}

			gridSearchResults.Rows.Clear();

			int maxRecords = 200;

			for (int i = 0; i < lstSearchFields.CheckedItems.Count; i++)
			{
				if (maxRecords <= 0) 
				{ 
					MessageBox.Show("Your search exceeded 200 items. Please narrow your search");
					break;
				}

				switch (lstSearchFields.CheckedItems[i].ToString())
				{
					case "Attachments":
						maxRecords -= SearchAttachments(txtSearchString.Text, maxRecords);
						break;
					case "Case Steps":
						maxRecords -= SearchCaseSteps(txtSearchString.Text, maxRecords);
						break;
					case "Case Titles":
						maxRecords -= SearchCaseTitles(txtSearchString.Text, maxRecords);
						break;
					case "Plan Reference Number":
						maxRecords -= SearchPlanRefNumbers(txtSearchString.Text, maxRecords);
						break;
					case "Plan Titles":
						maxRecords -= SearchPlanTitles(txtSearchString.Text, maxRecords);
						break;
					case "Result Notes":
						maxRecords -= SearchResultNotes(txtSearchString.Text, maxRecords);
						break;
					case "Result Reference Number":
						maxRecords -= SearchResultRefNumbers(txtSearchString.Text, maxRecords);
						break;
					case "Suite Setup":
						maxRecords -= SearchSuiteSetups(txtSearchString.Text, maxRecords);
						break;
					case "Suite Titles":
						maxRecords -= SearchSuiteTitles(txtSearchString.Text, maxRecords);
						break;
				}
			}
		}

		private void PopulateSearchResults(DataTable table, string searchLocation, string displayColumn)
		{
			if (table != null && table.Rows.Count > 0)
			{
				for (int i = 0; i < table.Rows.Count; i++)
				{
					string[] rowValues = new string[gridSearchResults.Columns.Count];
					rowValues[0] = searchLocation;
					rowValues[1] = table.Rows[i][displayColumn].ToString();

					DataGridViewRow row = new DataGridViewRow();
					row.CreateCells(gridSearchResults);
					row.SetValues(rowValues);
					row.Tag = table.Rows[i]["ID"].ToString();	//TODO: set to ID so it can be found later.

					gridSearchResults.Rows.Add(row);
				}
			}
		}

		private int SearchAttachments(string searchString, int maxRecords)
		{
			DataTable table = data.SearchAttachments(searchString, maxRecords);
			PopulateSearchResults(table, "Attachment", "FileName");

			return table.Rows.Count;
		}

		private int SearchCaseSteps(string searchString, int maxRecords)
		{
			DataTable table = data.SearchCaseSteps(searchString, maxRecords);
			PopulateSearchResults(table, "Case Steps", "Steps");

			return table.Rows.Count;
		}

		private int SearchCaseTitles(string searchString, int maxRecords)
		{
			DataTable table = data.SearchCaseTitles(searchString, maxRecords);
			PopulateSearchResults(table, "Case Title", "Description");

			return table.Rows.Count;
		}

		private int SearchPlanRefNumbers(string searchString, int maxRecords)
		{
			DataTable table = data.SearchPlanRefNumbers(searchString, maxRecords);
			PopulateSearchResults(table, "Plan Ref. Number", "ReferenceNumber");

			return table.Rows.Count;
		}

		private int SearchPlanTitles(string searchString, int maxRecords)
		{
			DataTable table = data.SearchPlanTitles(searchString, maxRecords);
			PopulateSearchResults(table, "Plan Title", "Description");

			return table.Rows.Count;
		}

		private int SearchResultNotes(string searchString, int maxRecords)
		{
			DataTable table = data.SearchResultNotes(searchString, maxRecords);
			PopulateSearchResults(table, "Result Notes", "Notes");

			return table.Rows.Count;
		}

		private int SearchResultRefNumbers(string searchString, int maxRecords)
		{
			DataTable table = data.SearchResultRefNumbers(searchString, maxRecords);
			PopulateSearchResults(table, "Result Ref. Number", "ReferenceNumber");

			return table.Rows.Count;
		}

		private int SearchSuiteSetups(string searchString, int maxRecords)
		{
			DataTable table = data.SearchSuiteSetups(searchString, maxRecords);
			PopulateSearchResults(table, "Suite Setup", "Steps");

			return table.Rows.Count;
		}

		private int SearchSuiteTitles(string searchString, int maxRecords)
		{
			DataTable table = data.SearchSuiteTitles(searchString, maxRecords);
			PopulateSearchResults(table, "Suite Title", "Description");

			return table.Rows.Count;
		}

		private void btnSearch_Click(object sender, EventArgs e)
		{
			SearchAll();
		}

		private void gridSearchResults_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
		{
			DataTable table = null;
			string id = gridSearchResults.Rows[e.RowIndex].Tag.ToString();
			string resultType = gridSearchResults.Rows[e.RowIndex].Cells["colType"].Value.ToString();
			
			switch (resultType)
			{
				case "Attachment":
					table = data.LocateAttachment(id);
					break;
				case "Case Steps":
					table = data.LocateCaseSuite(id);
					break;
				case "Case Title":
					table = data.LocateCaseSuite(id);
					break;
				case "Plan Ref. Number":
					table = data.LocatePlan(id);
					break;
				case "Plan Title":
					table = data.LocatePlan(id);
					break;
				case "Result Notes":
					table = data.LocateResult(id);
					break;
				case "Result Ref. Number":
					table = data.LocateResult(id);
					break;
				case "Suite Setup":
					table = data.LocateCaseSuite(id);
					break;
				case "Suite Title":
					table = data.LocateCaseSuite(id);
					break;
			}

			if (table != null && table.Rows.Count > 0)
			{
				SearchParameters searchLoc = new SearchParameters();
				searchLoc.ReleaseID = table.Rows[0]["ReleaseID"].ToString();
				searchLoc.PlanID = table.Rows[0]["PlanID"].ToString();
				searchLoc.CaseSuiteID = table.Rows[0]["CaseID"].ToString();

				Search.Invoke(searchLoc);
			}

			
		}
	}
}
